﻿/// Copyright © 2013 Microsoft Corporation. 
/// This project is released under the Microsoft Public License (MS-PL)
///
namespace DataMorph.Generator
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading;

    using DataMorph.Common.Contracts;

    public class TagGenerator : ITagGenerator
    {
        private static readonly ThreadLocal<Random> Random = new ThreadLocal<Random>(() => new Random());

        public IList<Tag> Generate(int tagCount, IEnumerable<TagType> types = null)
        {
            if (tagCount <= 0)
            {
                throw new ArgumentException("Tag count must be greater than 0.");
            }

            IList<TagType> tagTypeList;
            if (types == null || !types.Any())
            {
                tagTypeList = Enum.GetValues(typeof(TagType)).Cast<TagType>().ToList();
            }
            else
            {
                tagTypeList = types as IList<TagType> ?? types.ToList();
            }

            var tags = new List<Tag>();
            for (var i = 0; i < tagCount; i++)
            {
                tags.Add(new Tag
                {
                    Type = tagTypeList[Random.Value.Next(0, tagTypeList.Count)]
                });
            }

            return tags;
        }
    }
}